String Slicing in Python: Extracting Substrings
String slicing is a powerful technique in Python that allows you to extract substrings (portions of a string) based on specific indices. Here's a breakdown of how it works:
Basic Slicing Syntax
Basic Syntax to slice a stringstring_variable[start:end:step]
Explanation :
⮚
string_variable: The string you want to slice from.
⮚
start (optional): The index of the first character to include in the substring (defaults to 0, the beginning of the string).
⮚
end (optional): The index of the character after the last character to include (not included in the substring).
⮚
step (optional): The number of steps to take between characters (defaults to 1, including every character).
Understanding Indices
⮚ Indices in Python strings start from 0 (zero-based indexing).
⮚ The last character has an index equal to the string's length minus 1.
Extracting a Substring
Creating a substring in python by slicing a string
original_string = "Tutorialsbox.com"
# Extract characters from index 0 (inclusive) to index 5 (exclusive):
substring = original_string[0:5]
print(substring)
Output
Tutor
Extracting from the Beginning
Slicing string from beginning in python
original_string="Tutorialsbox.com"
substring = original_string[:7]
print(substring)
Output
Tutoria
Extracting Until the End
Extract a string from another string by slicing in python
original_string="Tutoialsbox.com"
# Without specifying end, goes to the end of the string:
substring = original_string[7:]
print(substring)
Output
sbox.com
Extracting Everything
Extracting same string using slicing
original_string="Tutorialsbox.com"
# Slicing the entire string (same as the original string):
substring = original_string[:]
print(substring)
Output
Tutorialsbox.com
Reverse Slicing (Negative Indices)
⮚ Python supports negative indexing, starting from the end of the string.
⮚ -1 refers to the last character, -2 to the second-last character, and so on.
string slicing from reverse in python
original_string="Tutorialsbox.com"
substring = original_string[-10:]
print(substring)
Output
alsbox.com
Slicing with Steps
⮚ The step parameter specifies the number of characters to skip between included characters.
⮚ step=2 includes every other character.
Slicing string by steps in python
original_string="Tutorialsbox.com"
substring = original_string[::2]
print(substring)
Output
Ttraso.o
Remember:
⮚ Slicing creates a new string; it doesn't modify the original string.
⮚ Slicing out of bounds (e.g., original_string[100]) will result in an IndexError.
Example:
Example of slicing strings in python
name = "Tutorialsbox.com"
# Extract first name (assuming no spaces before):
first_name = name[:name.find(" ")]
# Extract last name (assuming no trailing spaces):
last_name = name[name.find(" ") + 1:]
# Reverse the entire string:
reversed_name = name[::-1]
print(first_name)
print(last_name)
print(reversed_name)
Output
Tutorialsbox.co
Tutorialsbox.com
moc.xobslairotuT
We hope this explanation clarifies string slicing in Python